package bookManager.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import bookManager.po.Book;
import bookManager.service.BookManager;
import bookManager.serviceImpl.BookManagerImpl;
import fwk.Controller;
import fwk.impl.ModelAndView;

public class AddBook implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			BookManager bookManager=new BookManagerImpl();
			
			String bname=request.getParameter("bname");
			System.out.println(2333);
			double price=Double.parseDouble(request.getParameter("price"));
			System.out.println(2333);
			Date pressDate=formatter.parse(request.getParameter("pressDate"));
//		System.out.println(1);
			
			bookManager.addBook(new Book(bname, price, pressDate));
//			response.sendRedirect("showBooks");
			List<Book> bks=bookManager.findAllBooks();
//			System.out.println(2);
			Map<String, Object> map=new HashMap<String ,Object>();
			map.put("bks", bks);
			
			ModelAndView mv= new ModelAndView("listAllBooks",map);
			
			return mv;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		
		System.out.println(111);
		return null;
	}
	
}
